﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using Meteorite;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using ChaseCameraSample;

using OpenXLive;
using OpenXLive.Forms;
using OpenXLive.Features;
using Microsoft.Xna.Framework.Input;
using CocosDenshion;

namespace Meteorite
{
    public class GameOverScene : CCScene
    {        
        CCLabelBMFont Title;
        CCLabelBMFont Score;
        CCMenuItemLabel ButtonPlayAgain;
        CCMenuItemLabel ButtonMenu;
        

        public GameOverScene(int nScore)
        {
           
            CCSize size = CCDirector.sharedDirector().getWinSize();

            //Title
            Title = CCLabelBMFont.labelWithString("Game Over", GameConstant.Font_Futura_48);
            addChild(Title);
            Title.position = new CCPoint(size.width / 2, size.height);
            Title.scale = 2.0f;

            //Title
            Score = CCLabelBMFont.labelWithString("Your Score:"+nScore.ToString(), GameConstant.Font_Futura_48);
            addChild(Score);
            Score.position = new CCPoint(size.width / 2, -size.height);

            //Menu
            CCLabelBMFont LabelPlay = CCLabelBMFont.labelWithString("Play Again", GameConstant.Font_Futura_48);
            ButtonPlayAgain = CCMenuItemLabel.itemWithLabel(LabelPlay, ButtonPlayAgain, this.btnCallback);
            ButtonPlayAgain.position = new CCPoint(size.width / 2, -size.height);

            CCLabelBMFont LabelSetting = CCLabelBMFont.labelWithString("Menu", GameConstant.Font_Futura_48);
            ButtonMenu = CCMenuItemLabel.itemWithLabel(LabelSetting, ButtonMenu, this.btnCallback);
            ButtonMenu.position = new CCPoint(size.width / 2, -size.height);

            //Menu
            CCMenu pMenu = CCMenu.menuWithItems(ButtonPlayAgain, ButtonMenu);
            pMenu.position = new CCPoint(0, 0);
            addChild(pMenu);

            MenuGetIn();
        }
        public override void onEnter()
        {
            base.onEnter();


        }

        private void MenuGetIn()
        {
            CCSize size = CCDirector.sharedDirector().getWinSize();
            Title.runAction(CCMoveTo.actionWithDuration(0.8f, new CCPoint(size.width / 2, size.height - 100)));

            Score.runAction(CCMoveTo.actionWithDuration(0.5f, new CCPoint(size.width / 2, size.height - 250)));

            ButtonPlayAgain.runAction(CCMoveTo.actionWithDuration(0.6f, new CCPoint(size.width / 2, size.height - 300)));
            ButtonMenu.runAction(CCMoveTo.actionWithDuration(0.7f, new CCPoint(size.width / 2, size.height - 350)));

        }
        private void MenuGetOut()
        {
            CCSize size = CCDirector.sharedDirector().getWinSize();
            Title.runAction(CCMoveTo.actionWithDuration(0.8f, new CCPoint(size.width / 2, size.height + 60)));

            Score.runAction(CCMoveTo.actionWithDuration(0.8f, new CCPoint(size.width / 2, -size.height)));

            ButtonPlayAgain.runAction(CCMoveTo.actionWithDuration(0.5f, new CCPoint(size.width / 2, -size.height)));
            ButtonMenu.runAction(CCMoveTo.actionWithDuration(0.2f, new CCPoint(size.width / 2, -size.height)));
        }
        public void btnCallback(CCObject pSender)
        {
            if (pSender == ButtonPlayAgain)
            {
                MenuGetOut();
                runAction(CCSequence.actions(CCDelayTime.actionWithDuration(1.0f), CCCallFunc.actionWithTarget(this, new SEL_CallFunc(callback1))));
            }
            else if (pSender == ButtonMenu)
            {
                MenuGetOut();
                runAction(CCSequence.actions(CCDelayTime.actionWithDuration(1.0f), CCCallFunc.actionWithTarget(this, new SEL_CallFunc(callback2))));
            }
            if (0 == UserData.Instance().GetValue(GameConstant.UserData_Sound))
            {
                SimpleAudioEngine.sharedEngine().playEffect(CCFileUtils.fullPathFromRelativePath(GameConstant.Sound_Btn));
            }
        }
        public void callback1()
        {
            CCScene pScene = new GameScene();
            CCDirector.sharedDirector().replaceScene(pScene);
        }
        public void callback2()
        {
            CCScene pScene = new MainMenuScene();
            CCDirector.sharedDirector().replaceScene(pScene);
        }
    }
}
